#include "eeprom_flash.h"
#include "User.h"

eeprom_flash eeprom;

u8 location_mode;//0网络位置 1精确位置
String longitude;//经度
String latitude;//纬度
String location_text;//地区名
String location_up_text;//上级地区名
String location_text_buf = "";


void eeprom_flash::data_init()
{
    u8 x;
    EEPROM.begin(eeprom_size);
    //EEPROM初始化数据
    EEPROM.write(0,0);
    EEPROM.write(led_color_mode_add,1);
    EEPROM.write(led_color_add,0);
    EEPROM.write(led_color_mix_add,5);
    EEPROM.write(low_power_flg_add,1);
    EEPROM.write(night_shudown_add,1);
    EEPROM.write(sleep_hour_add,23);
    EEPROM.write(wake_hour_add,7);
    EEPROM.write(location_add,0);

    for(x = long_start_add;x < eeprom_size;x++)
        EEPROM.write(x,0);
    EEPROM.commit();
    EEPROM.end();
}
void eeprom_flash::read_all_data()
{
    u8 x;
    char i;
    EEPROM.begin(eeprom_size);

    if(EEPROM.read(0))
    {
        data_init();
        EEPROM.begin(eeprom_size);
    }

    led.color_choose_mode  = EEPROM.read(led_color_mode_add);
    led.color_mode_buf  = EEPROM.read(led_color_add);
    led.color_mix_buf  = EEPROM.read(led_color_mix_add);
    sleep_flg = EEPROM.read(low_power_flg_add);
    ui.night_shutdown_flg = EEPROM.read(night_shudown_add);
    ui.sleep_hour = EEPROM.read(sleep_hour_add);
    ui.wake_hour = EEPROM.read(wake_hour_add);
    location_mode = EEPROM.read(location_add);

    longitude = "";
    latitude = "";
    location_text = "";
    location_up_text = "";

    x = long_start_add;
    for(;;)
    {
        i = EEPROM.read(x++);

        if(x == lat_start_add)
            break;
        if(i == '\0')
            break;

        longitude += i;
    }
    x = lat_start_add;
    for(;;)
    {
        i = EEPROM.read(x++);
        if(x == location_start_add)
            break;
        if(i == '\0')
            break;
        latitude += i;
    }
    x = location_start_add;
    for(;;)
    {
        i = EEPROM.read(x++);
        if(x == eeprom_size - 1)
            break;
        if(i == '\0')
            break;
        location_text += i;
    }

    x = location_up_start_add;
    for(;;)
    {
        i = EEPROM.read(x++);
        if(x == eeprom_size - 1)
            break;
        if(i == '\0')
            break;
        location_up_text += i;
    }

    EEPROM.end();
    ui.led_color_set_flg = 1;
    #ifdef DEBUG
    {
        Serial.println("EEPROM DATA:");
        Serial.println(longitude);
        Serial.println(latitude);
        Serial.println(location_text);
        Serial.println(location_up_text);
    }
    #endif


}
u8 eeprom_flash::read(u8 add)
{
    u8 x;
    EEPROM.begin(eeprom_size);
    x=EEPROM.read(add);
    EEPROM.end();
    return x;
}
void eeprom_flash::write(u8 add,u8 dat)
{
    EEPROM.begin(eeprom_size);
    EEPROM.write(add,dat);
    EEPROM.commit();
    EEPROM.end();
}


void eeprom_flash::write_str(u8 add,String str)
{
    const char *p = &str[0];
    EEPROM.begin(eeprom_size);
    while(*p)
    {
        EEPROM.write(add++,*p++);
        yield();
    }
    EEPROM.write(add,'\0');
    EEPROM.commit();
    EEPROM.end();
}
